window.GenealogyConst = Object.freeze({
  StartRow: 2, // 数据表的开始行数
  EndRow: 200, // 数据表的结束行数
  EndCol: 20, // 数据表的最大列
  SelfPos: 1, // 自己所在位置
  SexPos: 2, // 性别位置
  FatherPos: 3,// 父亲所在位置
  MotherPos: 4, // 母亲所在位置
  MainLinePos: 7, // 主系位置
  GenealogyNamePos: 8, // 族名位置
  // 郎家辈分列表
  LangSeniorityList: ["祖先", "显", "承", "兴", "建", "宏", "业", "奇", "俊,", "才", '唯', '贤', '崇', '德', '勤', '至', '先', '家', '道', '施', '善', '举', '仁', '义', '勋', '照', '乾', '坤', '芳', '名', '传'],
})

window.GenealogyUtil = (function () {
  return {

    addGenealogySeniority(genealogyName = '郎') {
      const allData = EtUtils.getAllData(GenealogyConst.EndRow, GenealogyConst.EndCol);
      const selection = EtUtils.getSelection()
      const startRow = selection.Row;
      const endRow = (selection.Value2?.length || 1) + startRow - 1;
      const colPos = selection.Column;
      for (let i = startRow; i <= endRow; i++) {
        let count = 0;
        let curData = allData[i - 1];
        // 不是该族名的不统计
        if (genealogyName !== curData[GenealogyConst.GenealogyNamePos - 1]) {
          console.log(allData[i - 1][GenealogyConst.SelfPos - 1], '不是目标家族');
          continue;
        }

        while (curData && curData[GenealogyConst.FatherPos - 1] !== 'top') {
          count++;
          if (curData[GenealogyConst.MainLinePos - 1] === '母') {
            if (!curData[GenealogyConst.MotherPos - 1]) {
              break;
            }
            curData = allData.find(item => curData[GenealogyConst.MotherPos - 1] === (item[GenealogyConst.SelfPos - 1]));
          } else {
            if (!curData[GenealogyConst.FatherPos - 1]) {
              break;
            }
            curData = allData.find(item => curData[GenealogyConst.FatherPos - 1] === (item[GenealogyConst.SelfPos - 1]));
          }
        }


        if (curData && curData[GenealogyConst.FatherPos - 1] === 'top') {
          const result = GenealogyConst.LangSeniorityList[count];
          EtUtils.writeTextToCell(i, colPos, result);
        } else {
          console.log(allData[i - 1][GenealogyConst.SelfPos - 1], '没有找到父祖先');
        }
      }
    },

    addGenealogyLevel(genealogyName = '郎') {
      const allData = EtUtils.getAllData(GenealogyConst.EndRow, GenealogyConst.EndCol);
      const selection = EtUtils.getSelection()
      const startRow = selection.Row;
      const endRow = (selection.Value2?.length || 1) + startRow - 1;
      const colPos = selection.Column;
      for (let i = startRow; i <= Math.min(endRow, GenealogyConst.EndRow); i++) {
        let count = 0;
        let isOk = false;
        let curData = allData[i - 1];
        do {
          if (genealogyName !== curData[GenealogyConst.GenealogyNamePos - 1]) {
            const dataIdx = curData[GenealogyConst.MainLinePos - 1] === '父' ? GenealogyConst.MotherPos - 1 : GenealogyConst.FatherPos - 1;
            if (!curData[dataIdx]) {
              break;
            }
            curData = allData.find(item => curData[dataIdx] === (item[GenealogyConst.SelfPos - 1]));
          } else {
            isOk = true;
            break;
          }
          if (!curData) {
            break;
          }
          count++;
        } while (curData && curData[GenealogyConst.FatherPos - 1] !== 'top')
        if (isOk) {
          EtUtils.writeTextToCell(i, colPos, count);
        } else {
          console.log(allData[i - 1][GenealogyConst.SelfPos - 1], '找不到目标直系祖先');
        }
      }
    },

    addGenealogyChildren() {
      const allData = EtUtils.getAllData(GenealogyConst.EndRow, GenealogyConst.EndCol);
      const selection = EtUtils.getSelection()
      const startRow = selection.Row;
      const endRow = (selection.Value2?.length || 1) + startRow - 1;
      const colPos = selection.Column;
      for (let i = startRow; i <= Math.min(endRow, GenealogyConst.EndRow); i++) {
        const curInfo = allData[i - 1];
        const result = [];
        if (curInfo[GenealogyConst.SexPos - 1] === '男') {
          allData.forEach(item => {
            if (item[GenealogyConst.FatherPos - 1] === curInfo[GenealogyConst.SelfPos - 1]) {
              result.push(item[GenealogyConst.SelfPos - 1]);
            }
          })
        }
        if (curInfo[GenealogyConst.SexPos - 1] === '女') {
          allData.forEach(item => {
            if (item[GenealogyConst.MotherPos - 1] === curInfo[GenealogyConst.SelfPos - 1]) {
              result.push(item[GenealogyConst.SelfPos - 1]);
            }
          })
        }
        if (result.length) {
          EtUtils.writeTextToCell(i, colPos, result.join('、'));
        }
      }
    },


  }
})();